package com.earl.eatbao.service.impl;

import java.util.Random;

import com.earl.eatbao.util.CheckCodeUtils;
import com.earl.eatbao.util.SendCheckCodeUtil;
import com.earl.eatbao.util.StringBuilderUtil;
import com.taobao.api.ApiException;
import com.taobao.api.domain.BizResult;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.earl.eatbao.commons.util.EhCacheHelper;
import com.earl.eatbao.commons.util.SmsbaoHelper;
import com.earl.eatbao.exception.DomainSecurityException;
import com.earl.eatbao.service.VerifyCodeService;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

@Service("verifyCodeService")
@Transactional
public class VerifyCodeServiceImpl implements VerifyCodeService {
    private static Logger logger = Logger
            .getLogger(VerifyCodeServiceImpl.class);

    private static Cache VerifyCode_CACHE = EhCacheHelper.getCacheManage().getCache("verifyCode");

    /**
     * 发送手机验证码
     * @param phoneNumber
     */
    @Override
    public void sendVerifyCode(String phoneNumber){
         // 生成6位数验证码
         Random random = new Random();
         Integer mobileVerifyCode = random.nextInt(899999) + 100000;
         String code = Integer.toString(mobileVerifyCode);
         // 生成指定短信
         String mf = "[吃货宝]您的验证码是" + code + ",60秒有效";
         System.out.println(mf);
             int messageCode = SmsbaoHelper.send(phoneNumber, mf);
             messageCode = 0;
             //TODO 这里发布的时候需要修改
             if (messageCode != 0)
            	 throw new DomainSecurityException("短信发送失败");
             
             Element element = new Element(phoneNumber,code);
             VerifyCode_CACHE.put(element);
             logger.info("ehcache:" + element.getObjectValue());
    }

    @Override
    public Boolean getPhoneCode(String phoneNumber) {
        try {

            String code = StringBuilderUtil.getRandomNumberString(6);
            BizResult result = SendCheckCodeUtil.sendCheckCode(phoneNumber, code).getResult();
            logger.error("==== " + result.getErrCode() + "====");
            if (result.getSuccess()) {
                CheckCodeUtils.putCode(phoneNumber,code);
                return true;
            }else {
                return false;
            }
        } catch (ApiException e) {
            e.printStackTrace();
            logger.error(e);
        }
        return false;
    }

    @Override
    public String genVerifyCode(String phoneNumber) {
        sendVerifyCode(phoneNumber);
        String verifyCode = (String) VerifyCode_CACHE.get(phoneNumber).getObjectValue();
        return verifyCode;
    }

    @Override
    /**
     * test
     */
    public Boolean confirmVerifyCode(String verifyCode, String phoneNumber) {
        Element element = VerifyCode_CACHE.get(phoneNumber);
        if (element != null) {
            logger.info("------------------ehcache:" + element.getObjectValue());
            return verifyCode.equals(element.getObjectValue());
        } else {
            return false;
        }
    }
}
